package com.deeper.customview.ui.c5.exercise.c05e01;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.deeper.customview.R;

public class WaterMarkImageView extends ImageView {
    private static final int DEFAULT_POSITION = WATER_POSITION.UP_LEFT.ordinal();
    private Drawable waterDrawable;
    private String waterText;
    private int waterPosition;
    private int waterBmpWidth;
    private int waterBmpHeight;
    private Paint paint;
    private float waterTextSize;
    private int waterTextColor;
    private Bitmap waterBmp;
    private Canvas waterCanvas;
    private final int MARGIN = 20;


    public WaterMarkImageView(Context context) {
        super(context);
    }


    public WaterMarkImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public WaterMarkImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WaterMarkImageView);
        waterDrawable = typedArray.getDrawable(R.styleable.WaterMarkImageView_water_image);
        waterText = typedArray.getString(R.styleable.WaterMarkImageView_water_text);
        waterPosition = typedArray.getInt(R.styleable.WaterMarkImageView_water_position, DEFAULT_POSITION);
        typedArray.recycle();

        waterTextSize = 50;
        waterTextColor = Color.RED;

        paint = new Paint();
        initPaint();

        Rect textRect = getTextRect();

        waterBmpWidth = 300;
        waterBmpHeight = waterBmpWidth - textRect.height();
    }


    private void initPaint() {
        paint.reset();
        paint.setTextSize(waterTextSize);
        paint.setColor(waterTextColor);
    }


    private int measureWaterWidth(Rect waterTextRect) {
        return Math.max(waterBmpWidth, waterTextRect.width());
    }


    private int measureWaterHeight(Rect waterTextRect) {
        return waterBmpHeight + waterTextRect.height();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();

        createWaterBmp();

        int left = 0;
        int top = 0;
        switch (waterPosition) {
            case 0: //WATER_POSITION.UP_LEFT
                left = MARGIN;
                top = MARGIN;
                break;
            case 1: //WATER_POSITION.UP_RIGHT
                left = width - MARGIN - waterBmp.getWidth();
                top = MARGIN;
                break;
            case 2: //WATER_POSITION.DOWN_LEFT
                left = MARGIN;
                top = height - MARGIN - waterBmp.getHeight();
                break;
            default: //WATER_POSITION.DOWN_RIGHT
                left = width - MARGIN - waterBmp.getWidth();
                top = height - MARGIN - waterBmp.getHeight();
                break;
        }
        canvas.drawBitmap(waterBmp, left, top, paint);
    }


    private void createWaterBmp() {
        Rect waterTextRect = getTextRect();
        int waterWidth = measureWaterWidth(waterTextRect);
        int waterHeight = measureWaterHeight(waterTextRect);
        Log.d("CanvasTest", "createWaterBmp1: " + waterWidth + "   " + waterHeight);

        waterBmp = Bitmap.createBitmap(waterWidth, waterHeight, Bitmap.Config.ARGB_8888);
        waterCanvas = new Canvas(waterBmp);

        Bitmap waterDrawableBmp = ((BitmapDrawable) waterDrawable).getBitmap();
        Log.d("CanvasTest", "createWaterBmp: " + waterWidth + "   " + waterBmpWidth);
        int dx = (waterWidth - waterBmpWidth) / 2;
        int dy = 0;
        waterCanvas.save();
        waterCanvas.translate(dx, dy);
        waterCanvas.drawBitmap(waterDrawableBmp,
                new Rect(0, 0, waterDrawableBmp.getWidth(), waterDrawableBmp.getHeight()),
                new RectF(0, 0, waterBmpWidth, waterBmpHeight),
                null);
        waterCanvas.restore();

        //绘制文字
        paint.setColor(waterTextColor);
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        int x = (waterWidth - waterTextRect.width()) / 2;
        int y = (int) (waterTextRect.height() / 2 + (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent);
        waterCanvas.drawText(waterText, x, waterBmpHeight + y, paint);
    }


    private Rect getTextRect() {
        Rect rect = new Rect();
        paint.getTextBounds(waterText, 0, waterText.length(), rect);
        return rect;
    }
}
